﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace httphelper
{
    /// <summary>
    /// 关于文件的操作类
    /// </summary>
    public class FileUtil
    {
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static byte[] ReadFile(String filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open);
            byte[] bs = new byte[fs.Length];
            fs.Read(bs, 0, bs.Length);
            fs.Close();
            return bs;
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="data"></param>
        /// <param name="filename"></param>
        public static void SaveFile(byte[] data, String filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Create);
            fs.Write(data, 0, data.Length);
            fs.Close();
        }
        /// <summary>
        /// 从stream中读取并写入到文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="filename"></param>
        public static void WriteFile(Stream stream, String filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Create);

            byte[] data = new byte[1024];
            int len = 0;
            while ((len = stream.Read(data, 0, data.Length)) != 0)
            {
                fs.Write(data, 0, len);
            }

            fs.Close();
        }

       

    }
}
